import 'dart:math';
import 'dart:ui';

import 'package:easy_popup/easy_popup.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/widget/drop_down_widget/drop_down_body.dart';

import 'drop_down_menu.dart';

class DropDownPopWidget extends StatefulWidget {
  DropDownPopWidget({Key key}) : super(key: key);

  @override
  _DropDownPopWidgetState createState() => _DropDownPopWidgetState();
}

class _DropDownPopWidgetState extends State<DropDownPopWidget> {
  final List<String> items = ['中心选择', '适应症', '授课形式', '课程等级'];

  double offsetY;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: Colors.white,
      height: Adapt.px(44),
      child: DropDownHeader(
        items: items
            .map((e) => DropDownHeaderItem(
                  title: e,
                  index: items.indexOf(e),
                ))
            .toList(),
        onItemTap: (index) {
          int i = index > 1 ? 0 : 1;
          print(i);
          _onItemTap(context, type: DropDownBodyType.values[i]);
        },
      ),
    );
  }

  void _onItemTap(BuildContext context, {DropDownBodyType type}) {
    // 获取当前空前偏移量
    RenderBox renderBox = context.findRenderObject();
    var _offset = renderBox.localToGlobal(Offset.zero);
    print(_offset.dy);

    offsetY = _offset.dy + 44;
    EasyPopup.show(
      context,
      DropDownMenu(offsetY: offsetY, type: type),
      offsetLT: Offset(0, offsetY),
    );
  }
}

class DropDownMenu extends StatefulWidget with EasyPopupChild {
  final DropDownBodyType type;
  final _PopController controller = _PopController();
  final double offsetY;

  DropDownMenu({Key key, this.offsetY, this.type}) : super(key: key);

  @override
  _DropDownMenuState createState() => _DropDownMenuState();

  @override
  dismiss() {
    controller.dismiss();
  }
}

class _DropDownMenuState extends State<DropDownMenu>
    with SingleTickerProviderStateMixin {
  Animation<Offset> _animation;
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    widget.controller._bindState(this);
    _controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 300),
    );
    _animation = Tween<Offset>(begin: Offset(0, -1), end: Offset.zero)
        .animate(_controller);
    _controller.forward();
  }

  dismiss() {
    _controller?.reverse();
  }

  @override
  void dispose() {
    super.dispose();
    _controller?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 如果高度超过剩余距离，就显示剩余距离
    double kScreenHeight = MediaQueryData.fromWindow(window).size.height;
    double maxHeight = Adapt.px(322);
    double height = min(maxHeight, kScreenHeight - widget.offsetY - 30);

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        padding: EdgeInsets.only(top: widget.offsetY),
        child: ClipRect(
          child: SlideTransition(
            position: _animation,
            child: Container(
              height: height,
              color: Colors.white,
              child: Align(
                alignment: Alignment.topCenter,
                child: DropDownBody(
                  type: widget.type,
                  onTap: (_) {
                    Fluttertoast.showToast(msg: _);
                    EasyPopup.pop(context);
                  },
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class _PopController {
  _DropDownMenuState state;

  _bindState(_DropDownMenuState state) {
    this.state = state;
  }

  dismiss() {
    state?.dismiss();
  }
}

enum DropDownBodyType { single, mutal }
